<template>
    <popup
      :title="title"
      ref="popupRef"
      width="700"
      @onclose="close"
      :loading="loading"
      loadingText="信息提交中..."
      @onsubmit="submit"
    >
      <div class="form-wrapper">
        <el-form :model="serviceTypeForm" ref="formRef" :rules="rules">
          <el-form-item label="医生服务类型" prop="name">
            <el-input placeholder="请输入服务类型名称" v-model="serviceTypeForm.name" />
          </el-form-item>
          <el-form-item label="状态" prop="status">
    <el-select v-model="serviceTypeForm.status" placeholder="请选择状态">
      <el-option label="启用" :value="1" />
      <el-option label="禁用" :value="2" />
    </el-select>
  </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="submit">提交</el-button>
            <el-button @click="close">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </popup>
  </template>
  
  <script setup>
  import { ref, defineExpose, defineEmits } from "vue";
  import { addDoctorServiceTypeAPI, updateDoctorServiceTypeAPI } from "@/network/doctor_service_type"; // 替换为实际的 API 导入路径
  
  const emits = defineEmits(["close", "success"]);
  const title = ref("添加服务类型");
  const popupRef = ref(null);
  const formRef = ref(null);
  const loading = ref(false);
  const serviceTypeForm = ref({
  serviceTypeId: "",
  name: "",
  status: 1 // 默认启用
});
// 添加验证规则
const rules = {
  name: [{ required: true, message: "请输入医生服务类型", trigger: "blur" }],
  status: [{ required: true, message: "请选择状态", trigger: "change" }]
};
const open = async (row) => {
  popupRef.value?.open();
  serviceTypeForm.value = {
    serviceTypeId: "",
    name: "",
    status: 1
  };
  if (row.serviceTypeId) {
    title.value = "编辑服务类型";
    serviceTypeForm.value = {
      serviceTypeId: row.serviceTypeId,
      name: row.name,
      status: row.status
    };
  }
};
  const submit = async () => {
    formRef.value?.validate(async (validate) => {
      if (validate) {
        loading.value = true;
        try {
          if (serviceTypeForm.value.serviceTypeId) {
            // 编辑
            await updateDoctorServiceTypeAPI(serviceTypeForm.value);
          } else {
            // 新增
            await addDoctorServiceTypeAPI({ name: serviceTypeForm.value.name });
          }
          popupRef.value?.close();
          emits("success");
        } catch {
        } finally {
          loading.value = false;
        }
      }
    });
  };
  
  const close = () => {
    popupRef.value?.close();
    emits("close");
  };
  
  defineExpose({
    open,
  });
  </script>
  
  <style lang="less" scoped>
  .form-wrapper {
    padding: 10px 20px;
  }
  </style>